package xcmg.device.web.company;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.company.CompanySupplierService;
import xcmg.device.service.vo.company.CompanySupplierVO;
import xcmg.device.service.vo.company.InvitationCodeVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.util.StringUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by sqq on 2017/12/22.
 */
@RestController
@RequestMapping("/companySupplier")
public class CompanySupplierController extends BasicController {

    @Autowired
    private CompanySupplierService companySupplierService;

    @Autowired
    private HttpServletRequest request;

    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Supplier.ADD)
    public ActionResult<String> insert(@RequestBody CompanySupplierVO companySupplierVO)throws Exception{
        companySupplierVO.setOrgId(getOrgId());
        companySupplierVO.setCreateId(getUserId(request));
        companySupplierVO.setUpdateId(getUserId(request));
        companySupplierVO.setCreateName(getUserName());
        ErrorCode code=companySupplierService.insert(companySupplierVO);
        return actionResult(code);
    }

//    @RequestMapping(value = "/getSap",method = RequestMethod.GET)
//    @FastMappingInfo(needLogin = true,code = AuthCode.Supplier.GET_SAP)
//    public void getSap() throws Exception
//    {
//        companySupplierService.getSap(getOrgId());
//
//    }
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Supplier.EDIT)
    public ActionResult<String> update(@RequestBody CompanySupplierVO companySupplierVO){
        if(companySupplierVO.getId()==null||companySupplierVO.getId()<=0){
            return actionResult(ErrorCode.IllegalArument);
        }
        companySupplierVO.setUpdateId(getUserId(request));
        ErrorCode code=companySupplierService.update(companySupplierVO);
        return actionResult(code);
    }

    /**
     *
     * @return
     */
    @RequestMapping(value = "/bound",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Supplier.BIND)
    public  ActionResult<String> boundSupplier(@RequestBody InvitationCodeVO invitationCodeVO) throws Exception
    {
        if(null==invitationCodeVO|| StringUtil.isNullOrSpace(invitationCodeVO.getInvitationCode()))
        {
            return actionResult(ErrorCode.IllegalArument);
        }
        String orgId = getOrgId();
        return actionResult(companySupplierService.boundSupplier(orgId,invitationCodeVO.getInvitationCode()));
    }


    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Supplier.DEL)
    public ActionResult<String> delete(@RequestBody CompanySupplierVO companySupplierVO){
        if(companySupplierVO.getIds()==null){
            return actionResult(ErrorCode.IllegalArument);
        }
        companySupplierVO.setUpdateId(getUserId(request));
        ErrorCode code=companySupplierService.delete(companySupplierVO.getIds(),getUserId());
        return actionResult(code);
    }

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult<Object> list(@RequestBody SearchCommonVO<CompanySupplierVO> condition)throws Exception{
        CompanySupplierVO companySupplierVO=condition.getFilters();
        if(companySupplierVO==null){
            companySupplierVO=new CompanySupplierVO();
        }
        
        String orgId = getOrgId();
        if(StringUtil.isNullOrEmpty(companySupplierVO.getOrgId())){
            companySupplierVO.setOrgId(orgId);
        }
        if (companySupplierVO.getDeliveryOrgId() != null
                && (Integer.valueOf(1).equals(companySupplierVO.getIsDirect())||Integer.valueOf(1).equals(companySupplierVO.getIsDirectHF()))
                && !orgId.equals(companySupplierVO.getDeliveryOrgId())) {
            companySupplierVO.setOrgId(companySupplierVO.getDeliveryOrgId());
        }
        condition.setFilters(companySupplierVO);
        PageCommonVO<Object> pageCommonVO=companySupplierService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/listForPurchasePrice",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult<Object> listForPurchasePrice(@RequestBody SearchCommonVO<CompanySupplierVO> condition)throws Exception{
        return list(condition);
    }

    @RequestMapping(value = "/invite",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.Supplier.INVITE)
    public ActionResult<String> invite(@RequestBody CompanySupplierVO companySupplierVO){
        if(companySupplierVO.getId()==null||companySupplierVO.getId()<=0){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode code=companySupplierService.invite(companySupplierVO,getUserId(request));
        return  actionResult(code,companySupplierVO.getInvitationCode());
    }
    
    /**
     * 查询销售单位
     * @param condition
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/listSellUnit",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult listSellUnit(@RequestBody SearchCommonVO<CompanySupplierVO> condition) throws Exception {
        condition.getFilters().setOrgId(getOrgId());
        PageCommonVO<Object> pageCommonVO = companySupplierService.listSellUnit(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/isYn",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult<Boolean> isYn() throws Exception {
        return actionValueResult(companySupplierService.isYn(getOrgId()));
    }

}
